package leetcode.code1943;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public List<List<Long>> splitPainting(int[][] segments) {
		int len = segments.length;
		Map<Long, Long> map = new HashMap<>();
		for (int i = 0; i < len; i++) {
			int[] seg = segments[i];
			long l = seg[0], r = seg[1], v = seg[2];
			map.put(l, map.getOrDefault(l, 0L) + v);
			map.put(r, map.getOrDefault(r, 0L) - v);
		}
		int size = map.size();
		Long[][] points = new Long[size][2];
		int p = 0;
		for (Map.Entry<Long, Long> entry : map.entrySet()) {
			points[p][0] = entry.getKey();
			points[p][1] = entry.getValue();
			p++;
		}
		Arrays.sort(points, (a, b) -> a[0].compareTo(b[0]));
		long L = points[0][0];
		long value = points[0][1];
		List<List<Long>> ans = new ArrayList<>();
		for (int i = 1; i < size; i++) {
			Long[] point = points[i];
			Long R = point[0], diff = point[1];
			if (value != 0) {
				//（没有 被涂色的部分不出现在结果中）
				ans.add(Arrays.asList(new Long[] { L, R, value }));
			}
			L = R;
			value += diff;
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

	@Override
	public void debug4() {
		H.compare(H.str2array1(
				"[[2,3,10],[3,4,34],[4,9,46],[9,10,61],[10,11,36],[11,12,32],[12,13,35],[13,16,21],[18,19,13]]"),
				this.splitPainting(H.str2array1(
						"[[4,16,12],[9,10,15],[18,19,13],[3,13,20],[12,16,3],[2,10,10],[3,11,4],[13,16,6]]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

}
